package com.reactlibrary;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.ReadableType;
import com.facebook.react.bridge.WritableMap;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.common.ResContainer;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RNReactNativeMfbbShareModule extends ReactContextBaseJavaModule {

    private static Activity ma;
    private final int SUCCESS = 200;
    private final int ERROR = 0;
    private final int CANCEL = -1;
    private static Handler mSDKHandler = new Handler(Looper.getMainLooper());
    private ReactApplicationContext context;

    public RNReactNativeMfbbShareModule(ReactApplicationContext reactContext) {
        super(reactContext);
        this.context = reactContext;
    }

    @Override
    public String getName() {
        return "RNReactNativeMfbbShare";
    }

    public static void initSocialSDK(Activity activity) {
        ma = activity;
    }

    public static void onResult(Activity activity, int requestCode, int resultCode, Intent data) {
        UMShareAPI.get(activity).onActivityResult(requestCode, resultCode, data);
    }

    private static void runOnMainThread(Runnable runnable) {
        mSDKHandler.postDelayed(runnable, 0);
    }
    

    @ReactMethod
    public void share(final String text, final String img, final String weburl, final String title, final int sharemedia, final Callback successCallback) {
        runOnMainThread(new Runnable() {
            @Override
            public void run() {

                if (!TextUtils.isEmpty(weburl)) {
                    UMWeb web = new UMWeb(weburl);
                    web.setTitle(title);
                    web.setDescription(text);
                    if (getImage(img) != null) {
                        web.setThumb(getImage(img));
                    }
                    new ShareAction(ma).withText(text)
                            .withMedia(web)
                            .setPlatform(getShareMedia(sharemedia))
                            .setCallback(getUMShareListener(successCallback))
                            .share();
                } else if (getImage(img) != null) {
                    new ShareAction(ma).withText(text)
                            .withMedia(getImage(img))
                            .setPlatform(getShareMedia(sharemedia))
                            .setCallback(getUMShareListener(successCallback))
                            .share();
                } else {
                    new ShareAction(ma).withText(text)
                            .setPlatform(getShareMedia(sharemedia))
                            .setCallback(getUMShareListener(successCallback))
                            .share();
                }

            }
        });

    }

    private UMShareListener getUMShareListener(final Callback successCallback) {
        return new UMShareListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {

            }

            @Override
            public void onResult(SHARE_MEDIA share_media) {
                successCallback.invoke(SUCCESS, "success");
            }

            @Override
            public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                successCallback.invoke(ERROR, throwable.getMessage());
            }

            @Override
            public void onCancel(SHARE_MEDIA share_media) {
                successCallback.invoke(CANCEL, "cancel");
            }
        };
    }

    private UMImage getImage(String url) {
        if (TextUtils.isEmpty(url)) {
            return null;
        } else if (url.startsWith("http")) {
            return new UMImage(ma, url);
        } else if (url.startsWith("/")) {
            return new UMImage(ma, new File(url));
        } else if (url.startsWith("res")) {
            return new UMImage(ma, ResContainer.getResourceId(ma, "drawable", url.replace("res/", "")));
        } else {
            return new UMImage(ma, url);
        }
    }

    @ReactMethod
    public void auth(final int sharemedia, final Callback successCallback) {
        runOnMainThread(new Runnable() {
            @Override
            public void run() {
                UMShareAPI.get(ma).getPlatformInfo(ma, getShareMedia(sharemedia), new UMAuthListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {

                    }

                    @Override
                    public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
                        WritableMap result = Arguments.createMap();
                        for (String key : map.keySet()) {
                            result.putString(key, map.get(key));
                            Log.e("todoremove", "key=" + key + "   value" + map.get(key).toString());
                        }
                        successCallback.invoke(0, result, "success");
                    }

                    @Override
                    public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
                        WritableMap result = Arguments.createMap();
                        successCallback.invoke(1, result, throwable.getMessage());
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media, int i) {
                        WritableMap result = Arguments.createMap();
                        successCallback.invoke(2, result, "cancel");
                    }
                });
            }
        });

    }

    @ReactMethod
    public void shareboard(final String text, final String img, final String weburl, final String title, final ReadableArray sharemedias, final Callback successCallback) {
        runOnMainThread(new Runnable() {
            @Override
            public void run() {
                if (!TextUtils.isEmpty(weburl)) {
                    UMWeb web = new UMWeb(weburl);
                    web.setTitle(title);
                    web.setDescription(text);
                    if (getImage(img) != null) {
                        web.setThumb(getImage(img));
                    }
                    new ShareAction(ma).withText(text)
                            .withMedia(web)
                            .setDisplayList(getShareMedias(sharemedias))
                            .setCallback(getUMShareListener(successCallback))
                            .open();
                } else if (getImage(img) != null) {
                    new ShareAction(ma).withText(text)
                            .withMedia(getImage(img))
                            .setDisplayList(getShareMedias(sharemedias))
                            .setCallback(getUMShareListener(successCallback))
                            .open();
                } else {
                    new ShareAction(ma).withText(text)
                            .setDisplayList(getShareMedias(sharemedias))
                            .setCallback(getUMShareListener(successCallback))
                            .open();
                }

            }
        });

    }

    private SHARE_MEDIA getShareMedia(int num) {
        switch (num) {
            case 0:
                return SHARE_MEDIA.QQ;

            case 1:
                return SHARE_MEDIA.SINA;

            case 2:
                return SHARE_MEDIA.WEIXIN;

            case 3:
                return SHARE_MEDIA.WEIXIN_CIRCLE;
            case 4:
                return SHARE_MEDIA.QZONE;
//            case 5:
//                return SHARE_MEDIA.EMAIL;
//            case 6:
//                return SHARE_MEDIA.SMS;
//            case 7:
//                return SHARE_MEDIA.FACEBOOK;
//            case 8:
//                return SHARE_MEDIA.TWITTER;
//            case 9:
//                return SHARE_MEDIA.WEIXIN_FAVORITE;
//            case 10:
//                return SHARE_MEDIA.GOOGLEPLUS;
//            case 11:
//                return SHARE_MEDIA.RENREN;
//            case 12:
//                return SHARE_MEDIA.TENCENT;
//            case 13:
//                return SHARE_MEDIA.DOUBAN;
//            case 14:
//                return SHARE_MEDIA.FACEBOOK_MESSAGER;
//            case 15:
//                return SHARE_MEDIA.YIXIN;
//            case 16:
//                return SHARE_MEDIA.YIXIN_CIRCLE;
//            case 17:
//                return SHARE_MEDIA.INSTAGRAM;
//            case 18:
//                return SHARE_MEDIA.PINTEREST;
//            case 19:
//                return SHARE_MEDIA.EVERNOTE;
//            case 20:
//                return SHARE_MEDIA.POCKET;
//            case 21:
//                return SHARE_MEDIA.LINKEDIN;
//            case 22:
//                return SHARE_MEDIA.FOURSQUARE;
//            case 23:
//                return SHARE_MEDIA.YNOTE;
//            case 24:
//                return SHARE_MEDIA.WHATSAPP;
//            case 25:
//                return SHARE_MEDIA.LINE;
//            case 26:
//                return SHARE_MEDIA.FLICKR;
//            case 27:
//                return SHARE_MEDIA.TUMBLR;
//            case 28:
//                return SHARE_MEDIA.ALIPAY;
//            case 29:
//                return SHARE_MEDIA.KAKAO;
//            case 30:
//                return SHARE_MEDIA.DROPBOX;
//            case 31:
//                return SHARE_MEDIA.VKONTAKTE;
//            case 32:
//                return SHARE_MEDIA.DINGTALK;
//            case 33:
//                return SHARE_MEDIA.MORE;
            default:
                return SHARE_MEDIA.QQ;
        }
    }

    private SHARE_MEDIA[] getShareMedias(ReadableArray num) {
        SHARE_MEDIA[] medias = new SHARE_MEDIA[num.size()];
        for (int i = 0; i < num.size(); i++) {
            medias[i] = getShareMedia(num.getInt(i));
        }
        return medias;
    }


    /********************************U-App统计*********************************/
    @ReactMethod
    public void onPageStart(String pageName) {
        //android.util.Log.e("xxxxxx","onPageStart="+mPageName);
        MobclickAgent.onPageStart(pageName);
    }

    @ReactMethod
    public void onPageEnd(String pageName) {
        //android.util.Log.e("xxxxxx","onPageEnd="+mPageName);

        MobclickAgent.onPageEnd(pageName);

    }

    @ReactMethod
    public void onEvent(String eventId) {
        MobclickAgent.onEvent(context, eventId);
    }

    @ReactMethod
    public void onEventWithLable(String eventId, String eventLabel) {
        MobclickAgent.onEvent(context, eventId, eventLabel);
    }

    @ReactMethod
    public void onEventWithMap(String eventId, ReadableMap map) {
        Map<String, String> rMap = new HashMap<String, String>();
        ReadableMapKeySetIterator iterator = map.keySetIterator();
        while (iterator.hasNextKey()) {
            String key = iterator.nextKey();
            if (ReadableType.Array == map.getType(key)) {
                rMap.put(key, map.getArray(key).toString());
            } else if (ReadableType.Boolean == map.getType(key)) {
                rMap.put(key, String.valueOf(map.getBoolean(key)));
            } else if (ReadableType.Number == map.getType(key)) {
                rMap.put(key, String.valueOf(map.getInt(key)));
            } else if (ReadableType.String == map.getType(key)) {
                rMap.put(key, map.getString(key));
            } else if (ReadableType.Map == map.getType(key)) {
                rMap.put(key, map.getMap(key).toString());
            }
        }
        MobclickAgent.onEvent(context, eventId, rMap);
    }

    @ReactMethod
    public void onEventWithMapAndCount(String eventId, ReadableMap map, int value) {
        Map<String, String> rMap = new HashMap();
        ReadableMapKeySetIterator iterator = map.keySetIterator();
        while (iterator.hasNextKey()) {
            String key = iterator.nextKey();
            if (ReadableType.Array == map.getType(key)) {
                rMap.put(key, map.getArray(key).toString());
            } else if (ReadableType.Boolean == map.getType(key)) {
                rMap.put(key, String.valueOf(map.getBoolean(key)));
            } else if (ReadableType.Number == map.getType(key)) {
                rMap.put(key, String.valueOf(map.getInt(key)));
            } else if (ReadableType.String == map.getType(key)) {
                rMap.put(key, map.getString(key));
            } else if (ReadableType.Map == map.getType(key)) {
                rMap.put(key, map.getMap(key).toString());
            }
        }
        MobclickAgent.onEventValue(context, eventId, rMap, value);
    }

    @ReactMethod
    public void onEventObject(String eventID, ReadableMap property) {
        Map<String, Object> map = new HashMap();
        ReadableMapKeySetIterator iterator = property.keySetIterator();
        while (iterator.hasNextKey()) {
            String key = iterator.nextKey();
            if (ReadableType.Array == property.getType(key)) {
                map.put(key, property.getArray(key).toString());
            } else if (ReadableType.Boolean == property.getType(key)) {
                map.put(key, String.valueOf(property.getBoolean(key)));
            } else if (ReadableType.Number == property.getType(key)) {
                map.put(key, String.valueOf(property.getInt(key)));
            } else if (ReadableType.String == property.getType(key)) {
                map.put(key, property.getString(key));
            } else if (ReadableType.Map == property.getType(key)) {
                map.put(key, property.getMap(key).toString());
            }
        }

        MobclickAgent.onEventObject(context, eventID, map);

    }


    @ReactMethod
    public void unregisterPreProperty(String propertyName) {
        MobclickAgent.unregisterPreProperty(context, propertyName);

    }

    @ReactMethod
    public void getPreProperties(Callback callback) {
        String result = MobclickAgent.getPreProperties(context).toString();
        callback.invoke(result);
    }

    @ReactMethod
    public void clearPreProperties() {
        MobclickAgent.clearPreProperties(context);

    }

    @ReactMethod
    public void setFirstLaunchEvent(ReadableArray array) {
        List<String> list = new ArrayList();
        for (int i = 0; i < array.size(); i++) {
            if (ReadableType.Array == array.getType(i)) {
                list.add(array.getArray(i).toString());
            } else if (ReadableType.Boolean == array.getType(i)) {
                list.add(String.valueOf(array.getBoolean(i)));
            } else if (ReadableType.Number == array.getType(i)) {
                list.add(String.valueOf(array.getInt(i)));
            } else if (ReadableType.String == array.getType(i)) {
                list.add(array.getString(i));
            } else if (ReadableType.Map == array.getType(i)) {
                list.add(array.getMap(i).toString());
            }
        }
        MobclickAgent.setFirstLaunchEvent(context, list);
    }

    /********************************U-Dplus*********************************/
    @ReactMethod
    public void profileSignInWithPUID(String puid) {
        MobclickAgent.onProfileSignIn(puid);
    }

    @ReactMethod
    @SuppressWarnings("unused")
    public void profileSignInWithPUIDWithProvider(String provider, String puid) {
        MobclickAgent.onProfileSignIn(provider, puid);
    }

    @ReactMethod
    @SuppressWarnings("unused")
    public void profileSignOff() {
        MobclickAgent.onProfileSignOff();
    }


    @ReactMethod
    public void isInstall(String platform, Callback callback) {
        if ("1".equals(platform)) {
            boolean isInstallWX = isWeixinAvilible(context);
            if (isInstallWX) {
                callback.invoke(1, true);
            } else {
                callback.invoke(1, false);
            }
        } else if ("2".equals(platform)) {
            boolean isInstallWX = isQQClientAvailable(context);
            if (isInstallWX) {
                callback.invoke(2, true);
            } else {
                callback.invoke(2, false);
            }
        }
    }


    /**
     * 判断 用户是否安装微信客户端
     */
    public static boolean isWeixinAvilible(Context context) {
        final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mm")) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 判断 用户是否安装QQ客户端
     */
    public static boolean isQQClientAvailable(Context context) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equalsIgnoreCase("com.tencent.qqlite") || pn.equalsIgnoreCase("com.tencent.mobileqq")) {
                    return true;
                }
            }
        }
        return false;
    }

}